package com.seata.demo.order.controller;

import com.seata.demo.order.service.OrderService;
import com.seata.demo.order.service.SagaOrderService;
import com.seata.demo.order.service.TccOrderService;
import com.seata.demo.order.service.XaOrderService;
import io.seata.spring.annotation.GlobalTransactionalInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.math.BigDecimal;
import java.util.Map;

@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;
    
    @Autowired
    private TccOrderService tccOrderService;
    
    @Autowired
    private XaOrderService xaOrderService;
    
    @Autowired
    private SagaOrderService sagaOrderService;


    @Autowired
    private DataSource dataSource;

    @GetMapping("/checkDataSource")
    public String checkDataSource() {
        // 打印数据源的实际类型
        String dataSourceType = dataSource.getClass().getName();
        System.out.println("当前数据源类型：" + dataSourceType);

        // 若包含"SeataDataSourceProxy"则说明代理生效
        if (dataSourceType.contains("SeataDataSourceProxy")) {
            return "Seata数据源代理已生效，类型：" + dataSourceType;
        } else {
            return "Seata数据源代理未生效，当前类型：" + dataSourceType;
        }
    }



    /**
     * 测试AT模式
     */
    @GetMapping("/order/createAt")
    public String createOrderAt(
            @RequestParam("userId") String userId,
            @RequestParam("commodityCode") String commodityCode,
            @RequestParam("count") Integer count) {
        try {
            orderService.createOrder(userId, commodityCode, count);
            return "AT模式订单创建成功";
        } catch (Exception e) {
            return "AT模式订单创建失败: " + e.getMessage();
        }
    }


    /**
     * 测试TCC模式
     */
    @GetMapping("/order/createTcc")
    public String createOrderTcc(
            @RequestParam("userId") String userId,
            @RequestParam("commodityCode") String commodityCode,
            @RequestParam("count") Integer count) {
        try {

            BigDecimal money = new BigDecimal(count * 100); // 单价100元
            tccOrderService.createOrder(userId, commodityCode, count, money);
            return "TCC模式订单创建成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "TCC模式订单创建失败: " + e.getMessage();
        }
    }
    
    /**
     * 测试XA模式
     */
    @GetMapping("/order/createXa")
    public String createOrderXa(
            @RequestParam("userId") String userId,
            @RequestParam("commodityCode") String commodityCode,
            @RequestParam("count") Integer count) {
        try {
            xaOrderService.createOrder(userId, commodityCode, count);
            return "XA模式订单创建成功";
        } catch (Exception e) {
            return "XA模式订单创建失败: " + e.getMessage();
        }
    }
    
    /**
     * 测试Saga模式
     */
    @GetMapping("/order/createSaga")
    public String createOrderSaga(
            @RequestParam("userId") String userId,
            @RequestParam("commodityCode") String commodityCode,
            @RequestParam("count") Integer count) {
        try {
            BigDecimal money = new BigDecimal(count * 100); // 单价100元
            sagaOrderService.startSagaTransaction(userId, commodityCode, count, money);
            return "Saga模式订单创建成功";
        } catch (Exception e) {
            return "Saga模式订单创建失败: " + e.getMessage();
        }
    }
}
